package com.ybxx.fees.service.impl;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.ybxx.base.model.Room;
import com.ybxx.base.model.RoomInfo;
import com.ybxx.core.command.QueryFilter;
import com.ybxx.core.dao.GenericDao;
import com.ybxx.core.service.impl.BaseServiceImpl;
import com.ybxx.core.util.UUIDGenerator;
import com.ybxx.core.web.paging.PagingBean;
import com.ybxx.fees.dao.RecordsDao;
import com.ybxx.fees.model.AnmeterRecordItem;
import com.ybxx.fees.model.FfRecord;
import com.ybxx.fees.model.NqfRecord;
import com.ybxx.fees.model.OtherRecord;
import com.ybxx.fees.model.OtherRecordItem;
import com.ybxx.fees.model.Records;
import com.ybxx.fees.model.WatermeterRecordItem;
import com.ybxx.fees.model.WsfRecordItem;
import com.ybxx.fees.service.RecordsService;

public class RecordsServiceImpl extends BaseServiceImpl<Records> implements
		RecordsService {
	private RecordsDao recordsDao;
	public RecordsServiceImpl(RecordsDao dao) {
		super(dao);
		this.recordsDao=dao;
		// TODO Auto-generated constructor stub
	}
	public Records saveEdit(Records records) {
		/*修改电表数据状态*/
		String sql_1="update tbl_dashboard_ammeter_read set state='1' where rid='"+records.getRoom().getRid()+"' and ymonth='"+records.getYmonth()+"'";
		String sql_2="update tbl_dashboard_watermeter_read set state='1' where rid='"+records.getRoom().getRid()+"' and ymonth='"+records.getYmonth()+"'";
		recordsDao.excuteSql(sql_1);
		recordsDao.excuteSql(sql_2);
		return recordsDao.save(records);
	}
	public void confirm(Records records){
		recordsDao.excuteSql("update tbl_fees_records set state='1',realpay='"+records.getRealpay()+"'," +
				"barrearage='"+records.getBarrearage()+"'," +
				"content='"+records.getContent()+"',jtime=now(),charge='"+records.getCharge()+"',pname='"+records.getPname()+"' " +
						"where frcode='"+records.getFrcode()+"'");
	}
	public Records queryFees(String rid,String ymonth){
		String hql="from Records r left join fetch r.residents res " +
				"left join fetch r.otherRecords ros left join fetch ros.items " +
				"left join fetch r.glfRecords left join fetch r.nqfRecords " +
				"left join fetch r.ffRecords left join fetch r.hlfRecords " +
				"left join fetch r.anmeterRecords ras left join fetch ras.items " +
				"left join fetch r.watermeterRecords rws left join fetch rws.items " +
				"left join fetch r.wsfRecords rwf left join fetch rwf.items rwfs " +
				"left join fetch r.room room left join fetch room.unit u left join fetch u.ban b left join fetch b.community c "+
				"left join fetch  rwfs.feesType ft "+
				"where r.room.rid='"+rid+"' and r.ymonth='"+ymonth+"' and res.state='0'";
		return (Records)recordsDao.findUnique(hql, null);
	}
	public Records queryPrevFees(String unitid, String prevMonth) {
		String hql="from Records r " +
		           "where r.room.rid='"+unitid+"' and r.ymonth='"+prevMonth+"'";
		return (Records)recordsDao.findUnique(hql, null);
	}
	@Override
	public Records save(Records records) {
		/*删除当前的其它费用*/ 
		if(records.getOtherRecord()!=null && StringUtils.isNotEmpty(records.getOtherRecord().getOrcode())){
			recordsDao.excuteSql("delete from tbl_fees_other_record_item where orcode='"+records.getOtherRecord().getOrcode()+"'");
		}else{
			if(records.getOtherRecord()==null){
				records.setOtherRecord(new OtherRecord());
			}
			records.getOtherRecord().setOrcode(UUIDGenerator.getUUID());
		}
		/**整理其他费用*/
		for(Iterator<OtherRecordItem> iter=records.getOtherRecord().getItems().iterator();iter.hasNext();){
			OtherRecordItem item=iter.next();
			item.setRicode(UUIDGenerator.getUUID());
			item.setOtherRecord(records.getOtherRecord());
		}
		records.getOtherRecord().setRecords(records);
		/*整理暖气费*/
		NqfRecord nqfRecord=records.getNqfRecord();
		if(nqfRecord==null){
			nqfRecord=new NqfRecord();
			records.setNqfRecord(nqfRecord);
		}
		if(StringUtils.isEmpty(nqfRecord.getNqfrcode())){
			nqfRecord.setNqfrcode(UUIDGenerator.getUUID());
		}
		nqfRecord.setRecords(records);
		/*整理房费*/
		FfRecord ffRecord=records.getFfRecord();
		if(ffRecord==null){
			ffRecord=new FfRecord();
			records.setFfRecord(ffRecord);
		}
		if(StringUtils.isEmpty(ffRecord.getFfrcode())){
			ffRecord.setFfrcode(UUIDGenerator.getUUID());
		}
		ffRecord.setRecords(records);
		/*整理管理费*/
		if(records.getGlfRecord()!=null){
			records.getGlfRecord().setRecords(records);
		}
		/*整理护楼费*/
		if(records.getHlfRecord()!=null){
			records.getHlfRecord().setRecords(records);
		}
		/*整理卫生费*/
		if(records.getWsfRecord()!=null){
			for(Iterator<WsfRecordItem> iter=records.getWsfRecord().getItems().iterator();iter.hasNext();){
				WsfRecordItem item=iter.next();
				item.setWsfRecord(records.getWsfRecord());
			}
			records.getWsfRecord().setRecords(records);
		}
		
		/*整理水费*/
		if(records.getWatermeterRecord()!=null){
			for(Iterator<WatermeterRecordItem> iter=records.getWatermeterRecord().getItems().iterator();iter.hasNext();){
				WatermeterRecordItem item=iter.next();
				item.setWatermeterRecords(records.getWatermeterRecord());
			}
			records.getWatermeterRecord().setRecords(records);
		}
		/*整理电费*/
			if(records.getAnmeterRecord()!=null){
			for(Iterator<AnmeterRecordItem> iter=records.getAnmeterRecord().getItems().iterator();iter.hasNext();){
				AnmeterRecordItem item=iter.next();
				item.setAnmeterRecords(records.getAnmeterRecord());
			}
			records.getAnmeterRecord().setRecords(records);
		}
		return super.save(records);
	}
	public List<Records> queryFeesPage(QueryFilter filter, String ymonth,
			String unitid) {
		String hql="from Records r left join fetch r.residents res left join fetch r.room " +
		           "where r.ymonth='"+ymonth+"' ";
		if(unitid.startsWith("C")){
			hql+=" and r.room.unit.ban.community.cid='"+unitid.substring(1)+"'";
		}
		if(unitid.startsWith("B")){
			hql+=" and r.room.unit.ban.bid='"+unitid.substring(1)+"'";
		}
		if(unitid.startsWith("U")){
			hql+=" and r.room.unit.uid='"+unitid.substring(1)+"'";
		}
		return recordsDao.findByHql(hql, null, filter.getPagingBean());
	}
	

}
